<?php
class business_ctl_site_365webcall extends business_ctl_site_member{
    public function manage(){
        //设置不读缓存 add by weifeng 2013-10-28
        $GLOBALS['runtime']['nocache']=microtime();
        define('IS_POST', (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST'));
        $objMember = app::get('b2c')->model('members');
        $objStore = app::get('business')->model('storemanger');
        $host = defined('WEBCALL_HOST')?WEBCALL_HOST:'';
        $webcall_accountid = defined('WEBCALL_ACCOUNTID')?WEBCALL_ACCOUNTID:'';
        if(!IS_POST){
            $page_load = false;
            $store_info = $objStore->getList('store_id', array('account_id'=>$this->app_b2c->member_id));

            if(count($store_info) == 1) $this->member_post = 1;
            $member_info = $objMember->getList('email,mobile,im_webcall,allow_webcall', array('member_id'=>$this->app_b2c->member_id));
            $member_info = $member_info[0];
            //var_dump($member_info);
            if(!$this->member_post) $this->member_post = 2;
            $im_webcall = $member_info['im_webcall'];
            if(!$im_webcall)
            {
                if($this->member_post == 2 && $member_info['allow_webcall'] != 'true') unset($this->member_post);
                $im_webcall = empty($member_info['email'])?(empty($member_info['mobile'])?'':$member_info['mobile']):$member_info['email'];
                $page_load = true;
                if($this->member_post == 2){
                    $this->pagedata['is_member'] = $this->app_b2c->member_id;
                }
            }
            else
            {
                //$time = strtotime(date("Y-m-d H:i"));
                $time= date("YmdHi");
                $checkstring = substr(md5("ppsc$im_webcall$time"),0,16);
                $web_key = defined('WEBCALL_KEY')?WEBCALL_KEY:'';
                $webcall_center = "{$host}/manage_index.aspx?accountid=$webcall_accountid&userid=$im_webcall&timeStamp=$time$web_key";
                $checkstring = substr(md5($webcall_center),0,16);
                $webcall_center = "{$host}/manage_index.aspx?accountid=$webcall_accountid&userid=$im_webcall&timeStamp=$time&checkstring=$checkstring";
                $this->pagedata['webcall_center'] = $webcall_center;
                if($this->member_post == 1){
                    $objStoreM = app::get('business')->model('storemember');
                    $objAccount = app::get('pam')->model('account');
                    $member_info = $objStoreM->db->select("select s.member_id,a.login_name,m.mobile,m.email,m.name,m.im_webcall from {$objAccount->table_name(1)} as a join {$objMember->table_name(1)} as m on a.account_id=m.member_id join {$objStoreM->table_name(1)} as s on m.member_id=s.member_id and s.store_id='{$store_info[0]['store_id']}' where a.account_type='member' ");
                    
                    $new_webcall = array();
                    $old_webcall = array();
                    $member_json = array();
                    foreach((array)$member_info as $items){
                        if(empty($items['im_webcall'])){
                            $new_webcall[$items['member_id']] = $items['login_name'];
                            $member_json[$items['member_id']] = $items;
                        }else{
                            $old_webcall[] = $items;
                        }
                    }
                    $this->pagedata['new_webcall'] = $new_webcall;
                    $this->pagedata['old_webcall'] = $old_webcall;
                    $this->pagedata['member_json'] = json_encode($member_json);
                }
            }
            $this->pagedata['webcallHost'] = $host;
            $this->pagedata['allow_webcall'] = $this->member_post;
            $this->pagedata['page_load'] = $page_load;
            $this->pagedata['email'] = $im_webcall;
            $this->pagedata['_PAGE_'] = 'webcall.html';
            $this->output('business');
        }
        else
        {
            if($_POST['member_idd']){
                $params = array();
                $params['member_id'] = $_POST['member_idd'];
                $params['email'] = $_POST['email'];
                $params['pwd'] = $_POST['pwd'];
                $this->children_manage($params);
            }
            $reg_url = "{$host}/invite.aspx?email=".urlencode($_POST['email'])."&accountid=$webcall_accountid&pwd=".urlencode($_POST['pwd'])."&name=".urlencode("客服");


            //$httpclicnet = kernel::single('base_httpclient');
            //初始化
            $ch = curl_init();
            //设置选项，包括URL
            curl_setopt($ch, CURLOPT_URL, $reg_url);
            curl_setopt($ch, CURLOPT_HEADER, false);
            //执行
            $return = curl_exec($ch);
            //释放curl句柄
            curl_close($ch);
            $url = app::get('site')->router()->gen_url(array('app'=>'business','ctl'=>'site_365webcall','act'=>'manage'));
            if(intval($return)>0){
                $flag = $objMember->db->exec("update {$objMember->table_name(1)} set im_webcall = '{$_POST['email']}' where member_id = {$this->app_b2c->member_id}");
                if (!$flag)
                {
                    $this->splash('failed',$url , app::get('b2c')->_('自动开通客服帐号失败'),'','',false);
                }
                $this->splash('success',$url , app::get('b2c')->_('自动开通客服帐号成功'),'','',false);
            }else{
                $this->splash('failed',$url , app::get('b2c')->_('客服帐号已被占用，请检查！'),'','',false);
            }
        }
    }
    
    public function children(){
        $params = array();
        $params['member_id'] = $_POST['member_id'];
        $params['email'] = $_POST['email'];
        $params['pwd'] = $_POST['pwd'];
        $this->children_manage($params);
    }
    
    function children_manage($params){
        $host = defined('WEBCALL_HOST')?WEBCALL_HOST:'';
        $url = app::get('site')->router()->gen_url(array('app'=>'business','ctl'=>'site_365webcall','act'=>'manage'));
        if(!isset($params['member_id']) || empty($params['member_id'])){
            $this->splash('failed',$url , app::get('b2c')->_('店员为空，自动开通客服帐号失败'),'','',false);
        }
        $objMember = app::get('b2c')->model('members');
        $objStore = app::get('business')->model('storemanger');
        $objStoreM = app::get('business')->model('storemember');
        $objAccount = app::get('pam')->model('account');
        $store_member = $objStoreM->getList('store_id,member_id', array('member_id'=>$params['member_id']));
        $store_member = $store_member[0];
        if($store_member['store_id'] != $this->store_id) $this->splash('failed',$url , app::get('b2c')->_('非法操作，自动开通客服帐号失败'),'','',false);
        if(!$store_member['member_id']){
            $this->splash('failed',$url , app::get('b2c')->_('店员不存在，自动开通客服帐号失败'),'','',false);
        }
        $main_account = $objMember->db->select("select m.im_webcall,m.name from {$objMember->table_name(1)} as m join {$objStore->table_name(1)} as s on m.member_id=s.account_id and s.store_id='{$store_member['store_id']}'");
        $main_account = $main_account[0];
        if(!$main_account['im_webcall']){
            $this->splash('failed',$url , app::get('b2c')->_('店主没有开通主客服账号，自动开通客服帐号失败'),'','',false);
        }
        $reg_url = "{$host}/addSubAccount.aspx?mainAccount=".urlencode($main_account['im_webcall'])."&email=".urlencode($params['email'])."&accountid=$webcall_accountid&pwd=".urlencode($params['pwd'])."&name=".urlencode(($name = substr($main_account['name'], 0, 20))?$name:'客服');
        //$reg_url = urlencode($reg_url);

        //初始化
        $ch = curl_init();
        //设置选项，包括URL
        curl_setopt($ch, CURLOPT_URL, $reg_url);
        curl_setopt($ch, CURLOPT_HEADER, false);
        //执行
        $return = curl_exec($ch);
        //释放curl句柄
        curl_close($ch);
        if(intval($return)>0){
            $flag = $objMember->db->exec("update {$objMember->table_name(1)} set im_webcall = '{$params['email']}' where member_id = {$params['member_id']}");
            if (!$flag)
            {
                $this->splash('failed',$url , app::get('b2c')->_('自动开通客服帐号失败'),'','',false);
            }
            $this->splash('success',$url , app::get('b2c')->_('自动开通客服帐号成功'),'','',false);
        }else{
            $this->splash('failed',$url , app::get('b2c')->_('客服帐号已被占用，请检查！'),'','',false);
        }
    }
}
